<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>{if},{elseif},{else}</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="language.builtin.functions.html" title="Chapter 7. Built-in Functions">
<link rel="prev" href="language.function.function.html" title="{function}">
<link rel="next" href="language.function.include.html" title="{include}">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">{if},{elseif},{else}</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="language.function.function.html">Prev</a> </td>
<th width="60%" align="center">Chapter 7. Built-in Functions</th>
<td width="20%" align="right"> <a accesskey="n" href="language.function.include.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="{if},{elseif},{else}">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="language.function.if"></a>{if},{elseif},{else}</h2></div></div></div>
<p>
  <code class="varname">{if}</code> statements in Smarty have much the same flexibility as PHP
  <a class="ulink" href="http://php.net/if" target="_top">if</a>
  statements, with a few added features for the template engine.
  Every <code class="varname">{if}</code> must be paired with a matching
  <code class="varname">{/if}</code>. <code class="varname">{else}</code> and
  <code class="varname">{elseif}</code> are also permitted. All PHP conditionals
  and functions
  are recognized, such as <span class="emphasis"><em>||</em></span>, <span class="emphasis"><em>or</em></span>,
  <span class="emphasis"><em>&amp;&amp;</em></span>, <span class="emphasis"><em>and</em></span>,
  <span class="emphasis"><em>is_array()</em></span>, etc.
 </p>
<p>
  If securty is enabled, only PHP functions from <em class="parameter"><code>$php_functions</code></em> property of the securty policy are allowed. 
  See the <a class="link" href="advanced.features.html#advanced.features.security" title="Security">Security</a> section for details.
 </p>
<p>
  The following is a list of recognized qualifiers, which must be
  separated from surrounding elements by spaces. Note that items listed
  in [brackets] are optional. PHP equivalents are shown where applicable.
 </p>
<div class="informaltable"><table border="1">
<colgroup>
<col align="center">
<col align="center">
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th align="center">Qualifier</th>
<th align="center">Alternates</th>
<th>Syntax Example</th>
<th>Meaning</th>
<th>PHP Equivalent</th>
</tr></thead>
<tbody>
<tr>
<td align="center">==</td>
<td align="center">eq</td>
<td>$a eq $b</td>
<td>equals</td>
<td>==</td>
</tr>
<tr>
<td align="center">!=</td>
<td align="center">ne, neq</td>
<td>$a neq $b</td>
<td>not equals</td>
<td>!=</td>
</tr>
<tr>
<td align="center">&gt;</td>
<td align="center">gt</td>
<td>$a gt $b</td>
<td>greater than</td>
<td>&gt;</td>
</tr>
<tr>
<td align="center">&lt;</td>
<td align="center">lt</td>
<td>$a lt $b</td>
<td>less than</td>
<td>&lt;</td>
</tr>
<tr>
<td align="center">&gt;=</td>
<td align="center">gte, ge</td>
<td>$a ge $b</td>
<td>greater than or equal</td>
<td>&gt;=</td>
</tr>
<tr>
<td align="center">&lt;=</td>
<td align="center">lte, le</td>
<td>$a le $b</td>
<td>less than or equal</td>
<td>&lt;=</td>
</tr>
<tr>
<td align="center">===</td>
<td align="center"> </td>
<td>$a === 0</td>
<td>check for identity</td>
<td>===</td>
</tr>
<tr>
<td align="center">!</td>
<td align="center">not</td>
<td>not $a</td>
<td>negation (unary)</td>
<td>!</td>
</tr>
<tr>
<td align="center">%</td>
<td align="center">mod</td>
<td>$a mod $b</td>
<td>modulous</td>
<td>%</td>
</tr>
<tr>
<td align="center">is [not] div by</td>
<td align="center"> </td>
<td>$a is not div by 4</td>
<td>divisible by</td>
<td>$a % $b == 0</td>
</tr>
<tr>
<td align="center">is [not] even</td>
<td align="center"> </td>
<td>$a is not even</td>
<td>[not] an even number (unary)</td>
<td>$a % 2 == 0</td>
</tr>
<tr>
<td align="center">is [not] even by</td>
<td align="center"> </td>
<td>$a is not even by $b</td>
<td>grouping level [not] even</td>
<td>($a / $b) % 2 == 0</td>
</tr>
<tr>
<td align="center">is [not] odd</td>
<td align="center"> </td>
<td>$a is not odd</td>
<td>[not] an odd number (unary)</td>
<td>$a % 2 != 0</td>
</tr>
<tr>
<td align="center">is [not] odd by</td>
<td align="center"> </td>
<td>$a is not odd by $b</td>
<td>[not] an odd grouping</td>
<td>($a / $b) % 2 != 0</td>
</tr>
</tbody>
</table></div>
<div class="example">
<a name="id374060"></a><p class="title"><b>Example 7.44. {if} statements</b></p>
<div class="example-contents"><pre class="programlisting">

{if $name eq 'Fred'}
    Welcome Sir.
{elseif $name eq 'Wilma'}
    Welcome Ma'am.
{else}
    Welcome, whatever you are.
{/if}

{* an example with "or" logic *}
{if $name eq 'Fred' or $name eq 'Wilma'}
   ...
{/if}

{* same as above *}
{if $name == 'Fred' || $name == 'Wilma'}
   ...
{/if}


{* parenthesis are allowed *}
{if ( $amount &lt; 0 or $amount &gt; 1000 ) and $volume &gt;= #minVolAmt#}
   ...
{/if}


{* you can also embed php function calls *}
{if count($var) gt 0}
   ...
{/if}

{* check for array. *}
{if is_array($foo) }
   .....
{/if}

{* check for not null. *}
{if isset($foo) }
   .....
{/if}


{* test if values are even or odd *}
{if $var is even}
   ...
{/if}
{if $var is odd}
   ...
{/if}
{if $var is not odd}
   ...
{/if}


{* test if var is divisible by 4 *}
{if $var is div by 4}
   ...
{/if}


{*
  test if var is even, grouped by two. i.e.,
  0=even, 1=even, 2=odd, 3=odd, 4=even, 5=even, etc.
*}
{if $var is even by 2}
   ...
{/if}

{* 0=even, 1=even, 2=even, 3=odd, 4=odd, 5=odd, etc. *}
{if $var is even by 3}
   ...
{/if}

  </pre></div>
</div>
<br class="example-break"><div class="example">
<a name="id374073"></a><p class="title"><b>Example 7.45. {if} with more examples</b></p>
<div class="example-contents"><pre class="programlisting">
 
{if isset($name) &amp;&amp; $name == 'Blog'}
     {* do something *}
{elseif $name == $foo}
    {* do something *}
{/if}

{if is_array($foo) &amp;&amp; count($foo) &gt; 0}
    {* do a foreach loop *}
{/if}
  
</pre></div>
</div>
<br class="example-break">
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="language.function.function.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="language.builtin.functions.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="language.function.include.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">{function} </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> {include}</td>
</tr>
</table>
</div>
</body>
</html>
